package common

import (
	"context"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/dto/common/option_dto"
	repoCommonContract "git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/repository/common"
	commonPb "git.mycaigou.com/gfyx/micro-gfyx-api/proto/micro_gfyx_oppty_service_proto/micro-gfyx-oppty-service/common"
	"git.myscrm.cn/golang/stark/v4"
	"strings"
)

type optionService struct {
	optionRepo repoCommonContract.OptionRepositoryIface
}

// NewOptionService 创建构造函数
func NewOptionService(optionRepo repoCommonContract.OptionRepositoryIface) OptionServiceIface {
	return &optionService{
		optionRepo: optionRepo,
	}
}

func (s *optionService) RadarOpptyTypeOption(ctx context.Context, req *option_dto.GetRadarOpptyTypeOptionRequest) (*option_dto.GetRadarOpptyTypeOptionResponse, error) {
	resp, err := s.optionRepo.RadarOpptyTypeOption(ctx, &commonPb.GetRadarOpptyTypeOptionRequest{})
	if err != nil {
		return nil, err
	}

	result := &option_dto.GetRadarOpptyTypeOptionResponse{}
	if resp != nil {
		for _, option := range resp.Options {
			result.Options = append(result.Options, option_dto.Option{
				OpptyType:     option.OpptyType,
				OpptyTypeName: option.OpptyTypeName,
			})
		}
	}
	return result, nil
}
func (s *optionService) RadarAreaOption(ctx context.Context, req *option_dto.GetRadarAreaOptionRequest) (*option_dto.GetRadarAreaOptionResponse, error) {
	resp, err := s.optionRepo.RadarAreaOption(ctx, &commonPb.GetRadarAreaOptionRequest{})
	if err != nil {
		return nil, err
	}

	result := &option_dto.GetRadarAreaOptionResponse{
		Areas: []option_dto.Area{},
	}
	if resp != nil {
		for _, option := range resp.Options {
			result.Areas = append(result.Areas, option_dto.Area{
				Area:       option.Area,
				AreaCode:   option.AreaCode,
				ParentCode: option.ParentCode,
			})
		}
	}
	return result, nil
}

func (s *optionService) GetOption(ctx context.Context, req *option_dto.GetOptionRequest) (map[string][]*option_dto.OptionItem, error) {
	resp, err := s.optionRepo.GetOption(ctx, &commonPb.GetOptionRequest{
		OptionGroup: strings.Split(req.OptionGroup, ","),
	})
	if err != nil {
		return nil, err
	}
	result := &option_dto.GetOptionResponse{
		OptionGroupMap: make(map[string][]*option_dto.OptionItem),
	}
	if resp != nil {
		for _, option := range resp.Options {
			groupOptionList, ok := result.OptionGroupMap[option.BelongGroup]
			if ok {
				groupOptionList = append(groupOptionList, &option_dto.OptionItem{
					Text:        option.Text,
					Value:       option.Value,
					ParentValue: option.ParentValue,
				})
				result.OptionGroupMap[option.BelongGroup] = groupOptionList
			} else {
				result.OptionGroupMap[option.BelongGroup] = []*option_dto.OptionItem{{
					Text:        option.Text,
					Value:       option.Value,
					ParentValue: option.ParentValue,
				}}
			}
		}
	}
	return result.OptionGroupMap, nil
}

// 获取股权公司
func (s *optionService) GetInvestCompanyOption(ctx context.Context, request *option_dto.GetInvestCompanyRequest) (*option_dto.GetInvestCompanyResponse, error) {
	requestPb := &commonPb.GetInvestCompanyOptionRequest{
		OrganizingCode: request.OrganizingCode,
	}
	resPb, err := s.optionRepo.GetInvestCompanyOption(ctx, requestPb)
	if err != nil {
		stark.Logger.Error(ctx, "GetInvestCompanyOption，失败:", err)
		return nil, err
	}
	res := &option_dto.GetInvestCompanyResponse{}
	for _, item := range resPb.List {
		investCompanyItem := &option_dto.InvestCompanyItem{
			Value:  item.Value,
			Text:   item.Text,
			Parent: item.Parent,
		}
		res.List = append(res.List, investCompanyItem)
	}
	return res, nil
}
